home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / c-runtime / dispatch / record-inline.h < prev   
Encoding:
C/C++ Source or Header  |  1992-08-18  |  3.4 KB  |  141 lines

  1. /* -*-c-*- */
  2.  
  3. /* Copyright (C) 1989, 1992 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* As a special exception, if you link this library with files
  22.    compiled with GCC to produce an executable, this does not cause
  23.    the resulting executable to be covered by the GNU General Public License.
  24.    This exception does not however invalidate any other reasons why
  25.    the executable file might be covered by the GNU General Public License.  */
  26.  
  27. /* 
  28.  * $Header: /usr/user/dennis_glatting/ObjC/c-runtime/lib/RCS/objc-core.c,v 1.14
  29.  * 1992/01/03 02:55:03 dennisg Exp dennisg $ $Author: dglattin $ $Date:
  30.  * 1992/01/03 02:55:03 $ $Log: record-inline.h,v $
  31.  * Revision 1.3  1992/08/18  04:46:58  dglattin
  32.  * Saving a working version before release.
  33.  *
  34.  * Revision 1.2  1992/04/13  11:43:08  dennisg
  35.  * Check in after array version of run-time works.
  36.  * Expect more changes as hash version and other changes are made.
  37.  *
  38.  * Revision 1.1  1992/02/25  11:12:45  dennisg
  39.  * Initial revision
  40.  * 
  41.  */
  42.  
  43.  
  44. #ifndef _record_inline_INCLUDE_GNU
  45. #define _record_inline_INCLUDE_GNU
  46.  
  47. #include  <assert.h>
  48. #include  <stdlib.h>
  49. #include  <sys/types.h>
  50.  
  51.   /* Structure to hold records. */
  52.   typedef struct _Record {  
  53.     u_int capacity,
  54.           numEntries;
  55.     void* (*records)[];
  56.   } Record, *Record_t;
  57.   
  58.  
  59. /* Allocate, initialize and return a new record structure. */
  60. static inline Record_t
  61. record_new (void) {
  62.  
  63.   Record_t  newRecord;
  64.   
  65.   
  66.   newRecord = calloc (1, sizeof (Record));
  67.   assert(newRecord);
  68.   newRecord->capacity = 8;
  69.   newRecord->records = calloc (newRecord->capacity, sizeof (void*));
  70.   assert(newRecord->records);
  71.   
  72.   return newRecord;
  73. }
  74.  
  75.  
  76. /* Delete the record. */
  77. static inline void
  78. record_delete (Record_t record) {
  79.  
  80.   free (record->records);
  81.   free (record);
  82. }
  83.  
  84.  
  85. /* Return the number of entries in the reord. */
  86. static inline u_int
  87. record_entries (Record_t record) {
  88.  
  89.   return record->numEntries;
  90. }
  91.  
  92.  
  93. /* return the capacity of the record. */
  94. static inline u_int
  95. record_capacity (Record_t record) {
  96.  
  97.  
  98.   return record->capacity;
  99. }
  100.  
  101.  
  102. /* Store an entry at the specified record location. */
  103. static inline void
  104. record_store_at (u_int i, void* value, Record_t record) {
  105.  
  106.  
  107.   assert(i);
  108.   assert(i <= record_entries (record));
  109.  
  110.   (*record->records)[ i ] = value;
  111. }
  112.  
  113.  
  114. /* Make a record entry.  Expand the record's size if full. */
  115. static inline void
  116. record_store (void* value, Record_t record) {
  117.  
  118.  
  119.   ++record->numEntries;
  120.   if(record_entries (record) == record_capacity (record)) {
  121.     record->capacity *= 2;
  122.     record->records = 
  123.       realloc (record->records, (record_capacity (record) * sizeof (void*)));
  124.   }
  125.   record_store_at (record_entries (record), value, record);
  126. }
  127.  
  128.  
  129. /* get a value from the record. */
  130. static inline void*
  131. record_get (u_int i, Record_t record) {
  132.  
  133.  
  134.   assert( i );
  135.   assert( i <= record_entries (record));
  136.   return (*record->records)[ i ];
  137. }
  138.  
  139. #endif
  140.  
  141.